/**PlayGame.java
 * 
 * This is the driver of the program and the bulk of the game.
 * 
 * @author Christopher Carithers
 *Last Update: 5/14/2014
 *
 */

import java.lang.reflect.InvocationTargetException;
import java.util.Scanner;

public class PlayGame 
{
   public static final int SMALLEST_XPOSITION = 1;
   public static final int SMALLEST_YPOSITION = 1;
   public static final int LARGEST_XPOSITION = 16;
   public static final int LARGEST_YPOSITION = 16;
   
   public static final String NORTH_DIRECTION = "North";
   public static final String EAST_DIRECTION = "East";
   public static final String SOUTH_DIRECTION = "South";
   public static final String WEST_DIRECTION = "West";
   public static final String BACKPACK = "Backpack";
   
   public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException
   {
      Scanner selectOptionOnMenu = new Scanner(System.in);
	  boolean validOptionSelected = false;
	  int optionUserSelected = 2;
	   
	  //Creating the User Menu for RPG
	  UserMenu userMenu = new UserMenu("RPG User Menu\n", "User option does not exist choose another.");
	  MenuOption menuOption = new MenuOption(1, "1) Start New Game", "startNewGame");
	  MenuOption menuOption1 = new MenuOption(2, "2) Options", "options");
	  MenuOption menuOption2 = new MenuOption(3, "3) Exit Game", "exitGame");
	  userMenu.setMenuOptions(menuOption);
	  userMenu.setMenuOptions(menuOption1);
	  userMenu.setMenuOptions(menuOption2);
	  
	  while(optionUserSelected == 2)
	  {
	  
	     do
	     {
            System.out.println(userMenu.getTitleOfMenu());
		    userMenu.getMenuOptionsList();
		    optionUserSelected = selectOptionOnMenu.nextInt();
	        validOptionSelected = userMenu.executeSelectedMenuOption(optionUserSelected);
	      
	        if(!validOptionSelected)
	        {
	           userMenu.getOptionDoesNotExist();
	        }
	      
	     } while(!validOptionSelected);
	   
	     if(optionUserSelected == 1)
	     {
	        Map map = new Map("C:\\Users\\Christopher\\Development\\m653e292-textbasedrpg\\Maps\\map.dat", LARGEST_XPOSITION, LARGEST_YPOSITION);
	   
	        String playerName;
	        Scanner choosePlayerName = new Scanner(System.in);
	        System.out.println("\nChoose your Hero's name: ");
	        playerName = choosePlayerName.next();
	   
	        Player player = new Player(playerName);
	        Weapon weapon = new Weapon("Bronze Sword", "A weapon made of bronze to fight monster.");
	        player.setWeapons(weapon);
	        
	        System.out.println("\nYou have choosen your Hero's name to be: " + player.getPlayerName() + "\n\n");
	        System.out.println("Loading Map.....\n\n\n");
	        System.out.println("After typing a command you will press enter.\n");
	   
	        Scanner continueGame = new Scanner(System.in);
	        String continueGameString;
	   
	        do 
	        {
	           System.out.println("\nPlease type continue and press enter to continue...\n");
	           continueGameString = continueGame.nextLine();
	   
            } while(!continueGameString.equalsIgnoreCase("continue"));
	   
	        map.loadMap();
	        map.placePlayerToMap(player.getXPosition(), player.getYPosition());
	   
	        Scanner chooseOption = new Scanner(System.in);
	        String optionChosen = "Play";
	        while(!optionChosen.equalsIgnoreCase("Exit"))
	        {
	           map.printMap();
	           System.out.println("Exit Game: Exit   Move Up: North     Move Down: South");
	           System.out.println("Move Left: West   Move Right: East   Backpack: Backpack");
	           optionChosen = chooseOption.next();
	      
	           int coordinateNumber;
	           if(optionChosen.equalsIgnoreCase(NORTH_DIRECTION))
	           { 
	    	      if(player.getYPosition() <= SMALLEST_YPOSITION)
	    	      {
	    		     System.out.println("\nYou have reached the edge of the map.\nYou can not go anymore North.\n");
	    	      }
	    	      else if(map.mapSquareIsEmpty(player.getXPosition(), player.getYPosition()-1) == false)
	    	      {
	    		     System.out.println("Spot is occupied!");
	    		     if(map.objectOnMapSquare(player.getXPosition(), player.getYPosition()-1) == 'X')
	    		     {
	    		        System.out.println("There is a monster who wants to fight!");
	    		        System.out.println("Attack: Attack Flee: Flee");
	    		   
	    		        do
	    		        {
	    		           optionChosen = chooseOption.next();
	    		   
	    		           if(optionChosen.equalsIgnoreCase("Flee"))
	    		           {
	    			          System.out.println("\nYou have fleed from the monster!\n");
	    		           }
	    		           else if(optionChosen.equalsIgnoreCase("Attack"))
	    		           {
	    			          System.out.println("Get ready to fight!");
	    			          Monster monster = new Monster("Goblin", "A very ugly creature.");
	    			          System.out.println(monster.getMonsterName() + " " + monster.getMonsterDescription());
	    			          System.out.println("Combat is not implemented yet!");
	    		           }
	    		           else
	    		           {
	    			          System.out.println("That is not an option try again!");
	    			          System.out.println("Attack: Attack Flee: Flee");
	    			          optionChosen = "NotAnOption";
	    		           }
	    		        } while(optionChosen.equalsIgnoreCase("NotAnOption"));
	    		     }
	    	      }
	              else
	    	      {
	    		     player.movePlayerNorth();
	    		     map.updatePlayerOnMapNorth(player.getXPosition(), player.getYPosition());
	    	      }
	           }
	           if(optionChosen.equalsIgnoreCase(SOUTH_DIRECTION))
	           {
	    	      if(player.getYPosition() >= LARGEST_YPOSITION)
	    	      {
	    		     System.out.println("\nYou have reached the edge of the map.\nYou can not go anymore South.\n");
	    	      }
	    	      else if(map.mapSquareIsEmpty(player.getXPosition(), player.getYPosition()+1) == false)
	    	      {
	    		     System.out.println("Spot is occupied!");
	    		     if(map.objectOnMapSquare(player.getXPosition(), player.getYPosition()+1) == 'X')
	    		     {
	    		        System.out.println("There is a monster who wants to fight!");
	    		        System.out.println("Attack: Attack Flee: Flee");
	    		   
	    		        do
	    		        {
	    		           optionChosen = chooseOption.next();
	    		    
	    		           if(optionChosen.equalsIgnoreCase("Flee"))
	    		           {
	    			          System.out.println("\nYou have fleed from the monster!\n");
	    		           }
	    		           else if(optionChosen.equalsIgnoreCase("Attack"))
	    		           {
	    			          System.out.println("Get ready to fight!");
	         			      Monster monster = new Monster("Goblin", "A very ugly creature.");
	    			          System.out.println(monster.getMonsterName() + " " + monster.getMonsterDescription());
	    			          System.out.println("Combat is not implemented yet!");
	    		           }
	    		           else
	    		           {
	    			          System.out.println("That is not an option try again!");
	    			          System.out.println("Attack: Attack Flee: Flee");
	    			          optionChosen = "NotAnOption";
	    		           }
	    		        } while(optionChosen.equalsIgnoreCase("NotAnOption"));
	    		     }
	    	      }
	    	      else
	    	      {
	    		     player.movePlayerSouth();
	    		     map.updatePlayerOnMapSouth(player.getXPosition(), player.getYPosition());
	    	      }
	           }
	           if(optionChosen.equalsIgnoreCase(EAST_DIRECTION))
	           {
	    	      if(player.getXPosition() >= LARGEST_XPOSITION)
	    	      {
	    	         System.out.println("\nYou have reached the edge of the map.\nYou can not go anymore East.\n");
	    	       }
	    	       else if(map.mapSquareIsEmpty(player.getXPosition()+1, player.getYPosition()) == false)
	    	       {
	    		      System.out.println("Spot is occupied!");
	    		      if(map.objectOnMapSquare(player.getXPosition()+1, player.getYPosition()) == 'X')
	    		      {
	    		         System.out.println("There is a monster who wants to fight!");
	    		         System.out.println("Attack: Attack Flee: Flee");
	    		   
	    		         do
	    		         {
	    		            optionChosen = chooseOption.next();
	    		   
	    		            if(optionChosen.equalsIgnoreCase("Flee"))
	    		            {
	    			           System.out.println("\nYou have fleed from the monster!\n");
	    		            }
	    		            else if(optionChosen.equalsIgnoreCase("Attack"))
	    		            {
	    			           System.out.println("Get ready to fight!");
	    			           Monster monster = new Monster("Goblin", "A very ugly creature.");
	    			           System.out.println(monster.getMonsterName() + " " + monster.getMonsterDescription());
	    			           System.out.println("Combat is not implemented yet!");
	    		            }
	    		            else
	    		            {
	    			           System.out.println("That is not an option try again!");
	    			           System.out.println("Attack: Attack Flee: Flee");
	    			           optionChosen = "NotAnOption";
	    		            }
	    		         } while(optionChosen.equalsIgnoreCase("NotAnOption"));
	    		      }
	    	       }
	    	       else
	    	       {
                      player.movePlayerEast();
	    		      map.updatePlayerOnMapEast(player.getXPosition(), player.getYPosition());
	    	       }
	            }
	            if(optionChosen.equalsIgnoreCase(WEST_DIRECTION))
	            {
	    	       if(player.getXPosition() <= SMALLEST_XPOSITION)
	    	       {
	    		      System.out.println("\nYou have reached the edge of the map.\nYou can not go anymore West.\n");
	    	       }
	    	       else if(map.mapSquareIsEmpty(player.getXPosition()-1, player.getYPosition()) == false)
	    	       {
	    		      System.out.println("Spot is occupied!");
	    		      if(map.objectOnMapSquare(player.getXPosition()-1, player.getYPosition()) == 'X')
	    		      {
	    		         System.out.println("There is a monster who wants to fight!");
	    		         System.out.println("Attack: Attack Flee: Flee");
	    		   
	    		         do
	    		         {
	    		            optionChosen = chooseOption.next();
	    		   
	    		            if(optionChosen.equalsIgnoreCase("Flee"))
	    		            {
	    			           System.out.println("\nYou have fleed from the monster!\n");
	    		            }
	    		            else if(optionChosen.equalsIgnoreCase("Attack"))
	    		            {
	    			           System.out.println("Get ready to fight!");
	    			           Monster monster = new Monster("Goblin", "A very ugly creature.");
	    			           System.out.println(monster.getMonsterName() + " " + monster.getMonsterDescription());
	    			           System.out.println("Combat is not implemented yet!");
	    		            }
	    		            else
	    		            {
	    			           System.out.println("That is not an option try again!");
	    			           System.out.println("Attack: Attack Flee: Flee");
	    			           optionChosen = "NotAnOption";
	    		            }
	    		         } while(optionChosen.equalsIgnoreCase("NotAnOption"));
	    		      }
	    	       }
	    	       else
	    	       {
	    		      player.movePlayerWest();
	    		      map.updatePlayerOnMapWest(player.getXPosition(), player.getYPosition());
	    	       }
	            }
	            if(optionChosen.equalsIgnoreCase(BACKPACK))
	            {
	               System.out.println("\nWhich category?");
  		           System.out.println("Weapons: Weapons Armour: Armour (Not Implemented Yet!)");
  		           
  		           do
  		           {
  		              optionChosen = chooseOption.next();
  		   
  		              if(optionChosen.equalsIgnoreCase("Weapons"))
  		              {
  			             System.out.println("\nHere are your weapons: ");
  			             player.printWeapons();
  			             System.out.println("\n");
  			             
  			             System.out.println("\nWield: Wield Exit: Back");
  			             
  	  		             do
  	  		             {
  	  		                optionChosen = chooseOption.next();
  	  		   
  	  		                if(optionChosen.equalsIgnoreCase("Wield"))
  	  		                {
  	  		                   int weaponChosen;
  	   	  		               do
  	  	  		               {
  	   	  		            	  System.out.println("Choose your weapon number to wield to player: ");
  	   	  		                  weaponChosen = chooseOption.nextInt();
  	  	  		   
  	  	  		                  if(weaponChosen > 0 && weaponChosen <= player.getNumberOfWeaponsInBackpack())
  	  	  		                  {
  	  	  		                	 player.equipWeaponToPlayer(weaponChosen);
  	  	  			                 System.out.println("Weapon Equiped Now!\n");
  	  	  		                  }
  	  	  		                  else
  	  	  		                  {
  	  	  			                 System.out.println("That is not an option try again!");
  	  	  			                 System.out.println("Choose Another Weapon: ");
  	  	  			                 weaponChosen = 0;
  	  	  		                  }
  	  	  		               } while(weaponChosen == 0);
  	  		                }
  	  		                else if(optionChosen.equalsIgnoreCase("Back"))
  	  		                {
  	  		                   optionChosen = "Back";
  	  		                }
  	  		                else
  	  		                {
  	  			               System.out.println("That is not an option try again!");
  	  			               System.out.println("Wield: Wield ExitBackpack: Back");
  	  			               optionChosen = "NotAnOption";
  	  		                }
  	  		             } while(optionChosen.equalsIgnoreCase("NotAnOption"));
  		              }
  		              else if(optionChosen.equalsIgnoreCase("Armour"))
  		              {
  			             System.out.println("Not Implemented Yet!");
  		              }
  		              else
  		              {
  			             System.out.println("That is not an option try again!");
  			             System.out.println("Weapons: Weapons Armour: Armour");
  			             optionChosen = "NotAnOption";
  		              }
  		           } while(optionChosen.equalsIgnoreCase("NotAnOption"));
	            }
	         }
          }
	      else if(optionUserSelected == 2)
	      {
		     System.out.println("\nWelcome to Options Menu!");
		     System.out.println("This will describe on how to play the text-based RPG game.");
		     System.out.println("\nYou are the hero in this storyline. You must explore this strange world you have entered.");
		     System.out.println("Strange monsters inhabit this place and strange items will be found throughout the adventure.");
		     System.out.println("\nIn order to play through this game you will follow what the text says.");
		     System.out.println("For instance commands such as North, South, East, and West will show up,");
		     System.out.println("you will type what it says and press enter to do the action, so type North then press enter");
		     System.out.println("and you will move North.");
		     System.out.println("Same goes with other commands such as fighting or fleeing a monster, looking through your backpack, ");
		     System.out.println("using items, and wielding your equipment.");
		     System.out.println("Good luck adventurer!\n\n");
	      }
	      else
	      {
		     System.out.println("\nProblem with User Menu please restart game!\n");
	      }
       }
    }
}